package spider.com.fenwe.util;


import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StreamReader {
	static String path = null;
	static File file = null;
	static FileInputStream fis = null;
	static {
		path = "d:" + File.separator + "aaa.txt";
		file = new File(path);
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		byte[] b = new byte[10];
		int len = -1;
		while((len = fis.read(b))!=-1){
			System.out.println(new String(b,0,len));
			System.out.println("end:"+len);
		}
		//fileInputStream();
		//fileOutputStream();
		//fileCopy();
		//dataInputStream();
	}

	public static void fileInputStream() {
		byte[] b = new byte[10];
		int len = 0;
		StringBuffer sb = new StringBuffer();
		try {
			while ((len = fis.read(b)) != -1) {
				sb.append(new String(b, 0, len));
				System.out.println("ff: " + new String(b, 0, len));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(sb.toString());
	}

	public static void fileOutputStream(String filePath, String source) {
		FileOutputStream fos;
		try {
			File file = new File(filePath);
			fos = new FileOutputStream(file,true);
			fos.write(source.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void fileCopy() {
		File file2 = new File("d:"+file.separator+"aaa2.txt");
		try {
			FileOutputStream fos = new FileOutputStream(file2,true);
			byte[] b = new byte[255];
			int key = 0;
			while((key = fis.read(b)) != -1){
				fos.write(b);
			}
			//fos.write(data.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void dataInputStream() {
		DataInputStream dis = new DataInputStream(fis);
		char[] ch = new char[10];
		int count = 0;
		char temp;
		try {
			while ((temp = dis.readChar()) != 'C') {
				ch[count++] = temp;
			}
			System.out.println(ch);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
